/**
 * Created by arons on 16/6/29.
 */

function testRest(a, b){
    console.log(a);
    console.log(b);

    var rest = [];
    for(var i=2; i<arguments.length; i++){
        rest.push(arguments[i]);
    }
    console.log(rest);
}
//testRest(2, 3, 4, 5)

//function testRest(a, b, ...rest){
//    console.log(a);
//    console.log(b);
//    console.log(rest);
//}

function area_of_circle(r, pi){
    "use strict";
    if(arguments.length == 1){
        pi = 3.14;
    }
    return pi*r*r;
}

//// 测试:
//if (area_of_circle(2) === 12.56 && area_of_circle(2, 3.1416) === 12.5664) {
//    console.log('测试通过');
//} else {
//    console.log('测试失败');
//}

var abs = function (x) {
    if (x >= 0) {
        return x;
    } else {
        return -x;
    }
};

var absV = abs(-10);
console.log(absV);


//"use strict";
//
//var course = 'Learn JavaScript';
//console.log(course); // 'Learn JavaScript'
//console.log(course); // 'Learn JavaScript'

var stu = {
    name:"xiaomin",
    birth:1990,
    age: function(){
        "use strict";
        var y = new Date().getFullYear();
        return y-this.birth;
    }
}

//var stuAge = stu.age();
//console.log(stuAge);

function getAge() {
    var y = new Date().getFullYear();
    return y - this.birth;
}

var xiaoming = {
    name: '小明',
    birth: 1990,
    age: getAge
};

// Test apply
var xmAge = xiaoming.age(); // 25
getAge.apply(xiaoming, []); // 25, this指向xiaoming, 参数为空
console.log(xmAge);

function testApplyAndCall(){
    "use strict";
    // apply 参数打包成Array
    var maxV = Math.max.apply(null,[3, 4]);
    console.log(maxV);

    // call 参数按顺序传入
    var minV = Math.min.call(null, 2, 5);
    console.log(minV);
}

//testApplyAndCall();


// Test Apply Decoration
var count = 0;
var oldParseInt = parseInt;
parseInt = function(){
    count ++;
    return oldParseInt.apply(null, arguments);
}

// Test
parseInt('10');
parseInt('10');
parseInt('10');

console.log("callCount = "+count);


// 高阶函数,函数当做参数进行传递
console.log("高阶函数");
function multi(x){
    "use strict";
    return x*x;
}

function multiAdd(x, y, f){
    "use strict";
    return f(x)+f(y);
}

var multiAddV = multiAdd(2, 3, multi);
console.log(multiAddV);

